<?php

namespace App\Model;

use App\Comment\Comment;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class BizTableRegion extends Model
{
    public $timestamps = false;
    protected $table = 'biz_table_region';

    /**
     * 获取餐台区域的列表
     * @return mixed
     */
    public function getTableRegionListByBiz()
    {
        return BizTableRegion::where(['biz_id' => Comment::getBizId()]) -> get() -> toArray();
    }

    /**
     * 通过餐台区域的主键ID获取餐台区域的详细信息
     * @param $regionId
     * @return mixed
     */
    public function getTableRegionInfo($regionId)
    {
        return BizTableRegion::where(['biz_id' => Comment::getBizId(), 'id' => $regionId]) -> first() -> toArray();
    }

    /**
     * 通过餐台区域的主键修改餐台区域的信息
     * @param $id
     * @param $updateData
     * @return mixed
     */
    public function updateTableRegion($id, $updateData)
    {
        return BizTableRegion::where(['id' => $id]) -> update($updateData);
    }

    /**
     * 根据餐台区域的主键ID删除餐台信息
     * @param $id
     * @return mixed
     */
    public function delTableRegion($id)
    {
        DB::beginTransaction();
        try{
            BizTableRegion::where(['id' => $id]) -> delete();
            BizTable::where(['region_id' => $id]) -> delete();
            DB::commit();
            return true;
        }catch (\ErrorException $errorException){
            DB::rollBack();
            return false;
        }

    }

    /**
     * 添加餐台区域信息
     * @param $data
     * @return mixed
     */
    public function addTableRegion($data)
    {
        return BizTableRegion::insertGetId($data);
    }
}
